package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.pojo.SysRight;
import com.ktjy.service.ISysRightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-01-15
 */
@Controller
@RequestMapping("/right")
public class SysRightController {
    Logger logger = LoggerFactory.getLogger(SysRightController.class);
    @Autowired
    ISysRightService iSysRightService;

    @RequestMapping("/list")
    public String list(SysRight sysRight,
                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       Model model) {
        try {
            IPage<SysRight> pager = iSysRightService.findPage(sysRight, pageIndex, pageSize);
            model.addAttribute("pager", pager);

            //回显数据
            model.addAttribute("condition", sysRight);
        } catch (Exception e) {
            logger.error("SysUserController 中的 list 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "right/list";
    }

    /**
     * 跳转到新增页面
     *
     * @return
     */
    @GetMapping("/add")
    public String add(Model model) {
        try {
            //查询所有角色
            return "right/add";
        } catch (Exception e) {
            logger.error("SysUserController 中的 add 方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/save")
    public String save(SysRight sysRight) {
        try {
            boolean ok = iSysRightService.save(sysRight);
            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/right/list";
            } else {
                //跳回新增页面
                return "right/add";
            }
        } catch (Exception e) {
            logger.error("SysUserController 中的 save 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据类型查询权限
     * @param rightType
     * @return
     */
    @GetMapping("/findAllRights")
    @ResponseBody
    public List<SysRight> findAllRights(String rightType){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("right_type",rightType);
        return iSysRightService.list(qw);
    }

    /**
     * 跳转修改页面
     *
     * @param rightCode
     * @param model
     * @return
     */
    @GetMapping("/edit")
    public String edit(@RequestParam("rightCode") String rightCode, Model model) {
        try {
            //1.根据id查询资源信息
            SysRight right = iSysRightService.getById(rightCode);
            model.addAttribute("right", right);
            //2. 父级资源列表
            List<SysRight> rightList=null;
            if(right.getRightType().equals("Document")){
                QueryWrapper qw=new QueryWrapper();
                qw.eq("right_type","Folder");
                rightList = iSysRightService.list(qw);
            }else if(right.getRightType().equals("Button")){
                QueryWrapper qw=new QueryWrapper();
                qw.eq("right_type","Document");
                rightList = iSysRightService.list(qw);
            }
            model.addAttribute("rightList",rightList);
        } catch (Exception e) {
            logger.error("SysUserController 中的 toEdit 方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "right/edit";
    }

    @PostMapping("/update")
    public String update(SysRight sysRight) {
        try {
            boolean ok = iSysRightService.updateById(sysRight);
            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/right/list";
            } else {
                //跳回新增页面
                return "right/edit";
            }
        } catch (Exception e) {
            logger.error("SysUserController 中的 save 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }


    @DeleteMapping("/del/{rightCode}")
    @ResponseBody
    public Object del(@PathVariable("rightCode") String rightCode) {
        Map<String, Object> map = new HashMap<>();
        try {
            boolean ok = iSysRightService.removeById(rightCode);  //调用service的方法进行数据删除
            if (ok) {
                map.put("delResult", true);
            } else {
                map.put("delResult", false);
            }
        } catch (Exception e) {
            logger.error("SysUserController 中的 del 方法执行发生错误。", e);
            map.put("delResult", false);
        }
        return map;
    }

}
